/**
 * 
 */
package cn.springsoter.core.security.utils;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.core.util.ArrayUtil;
import cn.springsoter.core.security.constant.SecurityConstant;

/**
 * 脱敏规则工具类
 * 
 * @author kevin
 *
 */
public class RuleUtil {
	private RuleUtil() {
	}

	/**
	 * 保留前M后N规则
	 * 
	 * @param field
	 * @param m
	 * @param n
	 * @param replaceType
	 * @param replaceValue
	 * @return
	 */
	public static String keepLeftMAndRightN(final String field, final int m, final int n, final int replaceType,
			final String replaceValue) {
		if (StringUtils.isBlank(field)) {
			return null;
		}
		final int fullLength = StringUtils.length(field);

		final String left = StringUtils.left(field, m);
		final String right = StringUtils.right(field, n);

		final int leftLength = StringUtils.length(left);
		final int rightLength = StringUtils.length(right);

		final String mid = StringUtils.mid(field, leftLength, (fullLength - leftLength - rightLength));

		if(StringUtils.isNotBlank(mid)) {
			switch (replaceType) {
			case SecurityConstant.REPLACE_TYPE_1_1:// 1:1替换
				return StringUtils.rightPad(left, (leftLength + StringUtils.length(mid)), replaceValue) + right;
			case SecurityConstant.REPLACE_TYPE_N_1:// N:1替换
				return StringUtils.rightPad(left, (leftLength + StringUtils.length(replaceValue)), replaceValue) + right;
			}
		}
		return field;
	}

	/**
	 * 保留第X至Y规则
	 * 
	 * @param field
	 * @param x
	 * @param y
	 * @param replaceType
	 * @param replaceValue
	 * @return
	 */
	public static String keepXToY(final String field, final int x, final int y, final int replaceType,
			final String replaceValue) {
		if (StringUtils.isBlank(field)) {
			return null;
		}
		final int fullLength = StringUtils.length(field);

		final String left = StringUtils.left(field, x - 1);
		final String right = StringUtils.right(field, fullLength - y);

		final int leftLength = StringUtils.length(left);
		final int rightLength = StringUtils.length(right);

		final String mid = StringUtils.mid(field, x - 1, y - x + 1);

		final int midLength = StringUtils.length(mid);

		switch (replaceType) {
		case SecurityConstant.REPLACE_TYPE_1_1:// 1:1替换
			if (midLength > 0) {// 能找到
				return StringUtils.remove(StringUtils.leftPad(mid, leftLength + midLength, replaceValue), mid) + mid
						+ StringUtils.remove(StringUtils.rightPad(mid, rightLength + midLength, replaceValue), mid);
			} else if (midLength == 0) {// 找不到
				return field;
			}
		case SecurityConstant.REPLACE_TYPE_N_1:// N:1替换
			StringBuilder sb = new StringBuilder();
			if (midLength > 0) {// 能找到
				if (leftLength == 0 && rightLength > 0) {// 左边没有，右边有
					sb.append(mid);
					sb.append(replaceValue);
				} else if (rightLength == 0 && leftLength > 0) {// 右边没有，左边有
					sb.append(replaceValue);
					sb.append(mid);
				} else if (leftLength == 0 && rightLength == 0) {// 左右两边都没有
					sb.append(mid);
				} else {// 两边都有
					sb.append(replaceValue);
					sb.append(mid);
					sb.append(replaceValue);
				}
			} else if (midLength == 0) {// 找不到
				return field;
			}
			return sb.toString();
		}
		return field;
	}
	
	/**
	 * 关键词替换规则
	 * 
	 * @param field
	 * @param keywords
	 * @param replaceType
	 * @param replaceValue
	 * @return
	 */
	public static String replaceKeywords(final String field, final String[] keywordsArr, final int replaceType,
			final String replaceValue) {

		String[] replaceCharArr = new String[keywordsArr.length];

		switch (replaceType) {
		case SecurityConstant.REPLACE_TYPE_1_1:// 1:1替换
			for (int i = 0; i < keywordsArr.length; i++) {
				ArrayUtil.setOrAppend(replaceCharArr, i,
						StringUtils.rightPad(replaceValue, StringUtils.length(keywordsArr[i]), replaceValue));
			}
		case SecurityConstant.REPLACE_TYPE_N_1:// N:1替换
			for (int i = 0; i < keywordsArr.length; i++) {
				ArrayUtil.setOrAppend(replaceCharArr, i, replaceValue);
			}
		}
		return StringUtils.replaceEach(field, keywordsArr, replaceCharArr);
	}

	/**
	 * 全隐藏规则
	 * 
	 * @param field
	 * @param replaceType
	 * @param replaceValue
	 * @return
	 */
	public static String hideAll(final String field, final int replaceType, final String replaceValue) {
		
		switch (replaceType) {
		case SecurityConstant.REPLACE_TYPE_1_1:// 1:1替换
			return StringUtils.rightPad(StringUtils.EMPTY, StringUtils.length(field), replaceValue);
		case SecurityConstant.REPLACE_TYPE_N_1:// N:1替换
			return replaceValue;
		}
		return field;
	}
	
	/**
	 * 全显示规则
	 * 
	 * @param field
	 * @return
	 */
	public static String showAll(final String field) {
		return field;
	}
}
